package com.thinkit.bigdata.core.log;

import com.thinkit.bigdata.web.model.result.Constants;
import com.thinkit.bigdata.web.model.sec.User;
import com.thinkit.bigdata.web.model.sys.SysOperateLog;
import com.thinkit.bigdata.web.service.log.SystemOperateLogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * aop记录日志
 */
@Aspect
@Component
public class LogAspect {
    @Autowired
    private SystemOperateLogService logService;//日志记录Service


    @After("@annotation(com.thinkit.bigdata.core.log.Tag)")
    public void insert(JoinPoint joinPoint) throws Throwable {
        String operation = "添加";
        StringBuffer stringContent = name(joinPoint);//获取操作名字

        String content = content(joinPoint, operation, stringContent);//具体操作模块

        insertLog(content);//数据添加到数据库
    }

    @After("@annotation(com.thinkit.bigdata.core.log.TagUpdate)")
    public void update(JoinPoint joinPoint) throws Throwable {

        String operation = "修改";

        StringBuffer stringContent = name(joinPoint);//获取操作名字

        String content = content(joinPoint, operation, stringContent);//具体操作模块

        insertLog(content);//数据添加到数据库
    }


    @Before("@annotation(com.thinkit.bigdata.core.log.TagDel)")
    public void del(JoinPoint joinPoint) throws Throwable {

        String operation = "删除";

        StringBuffer stringContent = name(joinPoint);//获取操作名字

        String content = content(joinPoint, operation, stringContent);//具体操作模块

        insertLog(content);//数据添加到数据库

    }

    public StringBuffer name(JoinPoint joinPoint) {
        StringBuffer stringContent = new StringBuffer();
        if (joinPoint.getArgs() != null) {
            for (Object info : joinPoint.getArgs()) {
                // 获取对象的所有方法
                Method[] methods = info.getClass().getDeclaredMethods();
                // 遍历方法，判断get方法
                for (Method method : methods) {
                    String methodNames = method.getName();
                    // 判断是不是get方法
                    if (methodNames.equals("getKeyword") || methodNames.equals("getSpeakername")||methodNames.equals("getContent")||methodNames.equals("getUsername")||methodNames.equals("getShowname")){
                        try {
                            stringContent.append(method.invoke(info));
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

        return stringContent;
    }

    public void insertLog(String content) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute(Constants.SESSION_USER_INFO);
        //创建日志对象
        SysOperateLog log = new SysOperateLog();
        // 操作人
        log.setUserid(user.getId());
        log.setUsername(user.getUsername());
        // 操作时间
        log.setOpttime(new Date());
        // 操作内容
        log.setContent(content);
        logService.insertSelective(log);
    }

    public String content(JoinPoint joinPoint, String operation, StringBuffer stringContent) {
        //判断参数
        if (joinPoint.getArgs() == null) {//没有参数
            return "";
        }
        //获取方法名
        String methodName = joinPoint.getSignature().getDeclaringTypeName();
        methodName = methodName.substring(methodName.lastIndexOf(".") + 1);

        String content;
        if (methodName.equals("KeywordServiceImpl")) {
            content = "【配置管理】菜单栏的【关键词配置】模块进行【 " + operation + "】操作,内容【" + stringContent + "】";
        } else if (methodName.equals("KwResultServiceImpl")) {
            content = "【详情页面】模块进行【关键词结果】判定操作";
        } else if (methodName.equals("SpResultServiceImpl")) {
            content = "【详情页面】模块进行【说话人结果】判定操作";
        } else if (methodName.equals("SttResultServiceImpl")) {
            content = "【详情页面】模块的【语音转写】进行【 " + operation + "】操作,内容【" + stringContent + "】";
        } else if (methodName.equals("SpeakerServiceImpl")) {
            content = "【配置管理】菜单栏的【说话人配置】模块进行【 " + operation + "】操作,内容【" + stringContent + "】";
        }  else if (methodName.equals("UserServiceImpl")) {
            content = "【账户与安全】菜单栏的【用户管理】模块进行【 " + operation + "】操作,内容【" + stringContent + "】";
        } else if (methodName.equals("SecRoleServiceImpl")) {
            content = "【账户与安全】菜单栏的【角色管理】模块进行【 " + operation + "】操作,内容【" + stringContent + "】";
        } else if (methodName.equals("SecPermissionServiceImpl")) {
            content = "【账户与安全】菜单栏的【权限管理】模块进行【 " + operation + "】操作,内容【" + stringContent + "】";
        }  else if (methodName.equals("ComputeNodeServiceImpl")) {
            content = "【计算节点管理】模块进行【 " + operation + "】操作";
        } else {
            content = "新增加模块";
        }

        return content;
    }
}